package com.modelscope.ai.mapper;

import com.modelscope.ai.domain.entity.Visit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.modelscope.ai.domain.vo.VisitVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author Administrator
* @description 针对表【visit】的数据库操作Mapper
* @createDate 2024-03-15 10:56:30
* @Entity com.modelscope.ai.domain.entity.Visit
*/
@Mapper
public interface VisitMapper extends BaseMapper<Visit> {


//    @Select("SELECT " +
//            "    a.id AS id," +
//            "    a.title AS title," +
//            "  a.url as url," +
//            "  a.total as total," +
//            "    COUNT(DISTINCT ai.id) AS visited " +
//            "FROM " +
//            "    visit v " +
//            "JOIN " +
//            "    article_item ai ON v.article_itemId = ai.id " +
//            "JOIN article a ON ai.pid = a.id " +
//            "WHERE " +
//            "    v.user_id=#{uid} and v.is_delete = 0 and ai.is_delete=0 and a.is_delete=0 " +
//            "GROUP BY v.user_id, a.id " +
//            "ORDER BY v.user_id, a.id ")
    @Select("SELECT  " +
            "  sa.id,  " +
            "  a.url,  " +
            "  a.total,  " +
            "  a.id AS articleId,  " +
            "  a.title ,  " +
            "  COUNT( DISTINCT v.article_itemId ) AS visited   " +
            " FROM  " +
            "  subscribe_article sa  " +
            "  LEFT JOIN visit v ON sa.id = v.pid AND v.is_delete = 0   " +
            "  LEFT JOIN article a ON sa.article_id = a.id  " +
            "WHERE  " +
            "  sa.uid=#{uid} and sa.is_delete = 0   " +
            "GROUP BY  v.pid " +
            " ORDER BY sa.update_time DESC")
    List<VisitVo> getVisited(@Param("uid") Long uid);

}




